package com.mr.base.dao.hibernate3;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.mr.base.dao.ItemDao;
import com.mr.base.domain.Item;
import com.mr.base.domain.ItemType;
import com.skysz.framework.dao.hibernate3.AbstractDomainDao;

/**
 * 项目管理 dao
 * @author zhaow
 *
 */
public class ItemDaoImpl extends AbstractDomainDao<Item> implements ItemDao {
	private static final long serialVersionUID = -3575010266596387434L;

	@Override
	public Class<Item> getDomainClazz() {
		return Item.class;
	}

	/**
	 * 禁用项目
	 * @param itemId
	 */
	public void disableItem(Long itemId) {
		String hql = "update Item t set t.enable=false where t.id=?";
		Session session = this.getSession();
		Query query = session.createQuery(hql);
		query.setLong(0, itemId);
		query.executeUpdate();
		session.flush();
	}
	
	/**
	 * 禁用项目类型
	 * @param itemId
	 */
	public void disableItemType(Long itemTypeId) {
		String hql = "update ItemType t set t.enable=false where t.id=?";
		Session session = this.getSession();
		Query query = session.createQuery(hql);
		query.setLong(0, itemTypeId);
		query.executeUpdate();
		session.flush();
	}
	
	/**
	 * 得到下级所有子节点和自己
	 */
	@SuppressWarnings("unchecked")
	public List<ItemType> findItemTypeTree() {
		StringBuffer hql = new StringBuffer();
		hql.append("select c from ItemType c left join fetch c.parent p ");
		hql.append(" where  c.enable = true and c.hiberarchy like p.hiberarchy||'%' ");
		return (List<ItemType>) this.getHT().find(hql.toString(), new Object[] {  });
	}
	
	/**
	 * 查询父节点下存在多少子节点
	 * @param parentId
	 */
	public int getCountByParentId(Long parentId) {
		String hql = "select count(*) from ItemType t where t.parent.id=?";
		Query query = getSession().createQuery(hql);
		query.setLong(0, parentId);
		Long count = (Long) query.uniqueResult();
		return count.intValue();
	}
	
	/**
	 * 查询项目
	 * @param itemId
	 * @return
	 */
	public Item loadItemById(Long itemId) {
		String hql = "from Item t left join fetch t.itemType where t.id="+itemId;
		return (Item)this.utilsDao.loadObject(Item.class, hql);
	}
	
	/**
	 * 判断是否唯一
	 * @param it
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean isUniqueItem(Item it) {
//		String hql = "from Item t where 1=1 ";
//		
//		if (it.getQcode() != null && !"".equals(it.getQcode()) && !"".equals(it.getName()) && it.getName() != null) {
//			hql += " and ( t.qcode='" + it.getQcode() + "' or t.name = '" + it.getName() + "') ";
//		} else if (it.getName() != null) {
//			hql += " and t.name='" + it.getName() + "' ";
//		}else if (it.getQcode() != null) {
//			hql += " and t.qcode='" + it.getQcode() + "' ";
//		} else if (it.getId() != null && it.getId().intValue() != 0) {
//			hql += " and t.id != " + it.getId();
//		}
//		Query query = getSession().createQuery(hql);
//		List list = query.list();
//		this.getSession().evict(list);
//		
//		if (list == null || list.size() <= 0)
//			return true;
//		return false;
		return true;
	}
}
